package cn.vastbase.jdbc.advanced.io;

import org.junit.Test;
import org.postgresql.bulkload.BulkloadManager;
import org.postgresql.jdbc.PgConnection;

import java.sql.*;

public class BulkloadTestCase {

    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://172.16.55.175:6666/zxsdb?bulkloadCtlFile=E:\\bulkload.ctl";
    static String username = "zxs";
    static String passwd = "Vastdata#0730";
    static Statement stmt = null;
    static PreparedStatement pstmt = null;
    static ResultSet rs = null;

    public Connection conn() {
        try {
            Class.forName(cname);
            return conn = DriverManager.getConnection(url, username, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("[FAIL] conn database fail." + e.getMessage());
        }
        return null;

    }

    public void close() {
        try {
            /* 关闭结果集对象 */
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close resultSet fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭连接 */
            conn.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close connection fail." + e.getMessage());
        }
    }


    @Test
    public void testBulkloadWithServerLocalFileInput() throws SQLException {
        if (conn() == null)
            return;
        BulkloadManager bulkloadAPI = ((PgConnection) conn).getBulkloadAPI();
//        BulkloadProperties bulkloadInfo = new BulkloadProperties();
//        bulkloadInfo.put("IMP_FUNC", "vb");  // 导入方式选择，支持vb
////        bulkloadInfo.put("INPUT", "stdin");  // 数据源的路径，可以是文件路径、标准输入（stdin）或函数名称
//        bulkloadInfo.put("LIMIT", "INFINITE"); // 要加载的行数。默认值为INFINITE，即将加载所有数据。
//        bulkloadInfo.put("PARSE_ERRORS", "0");
//        bulkloadInfo.put("TYPE", "CSV"); //输入数据的类型， 支持可选项：CSV | BINARY | FIXED | FUNCTION，默认为CSV
//        bulkloadInfo.put("SKIP", "0"); // 跳过输入行的数量。默认值为 0。不能同时指定TYPE=FUNCTION和 SKIP。
//        bulkloadInfo.put("DELIMITER", "|");
//        bulkloadInfo.put("LOGFILE", "/home/vastbase/bulkload.log");
//        bulkloadInfo.put("OUTPUT", "public.test_bulkload"); // 导出文件或表名
//        bulkloadAPI.setBulkloadInfo(bulkloadInfo);
//        bulkloadAPI.getBulkloadInfo().put("INPUT", "/home/vastbase/bulkload_output.txt");
        try {
            ResultSet result = bulkloadAPI.bulkloadIn();
            while (result.next()) {
                for (int i = 1; i < result.getMetaData().getColumnCount() + 1; i++) {
                    System.out.println(result.getMetaData().getColumnLabel(i) + ":" + result.getObject(result.getMetaData().getColumnLabel(i)));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

}
